package com.smart.stock.module.maindata.config;

import com.smart.stock.common.util.h2.H2Util;
import com.smart.stock.common.util.spring.EntityUtil;
import com.smart.stock.module.maindata.entity.Stock;
import com.smart.stock.module.maindata.entity.StockCodeInfo;
import com.smart.stock.module.maindata.entity.StockCompany;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class InitData {

    @Autowired
    private MongoTemplate template;

    @Bean
    public String init(){
        try {
            String entityName = EntityUtil.getEntityNameByCode(StockCodeInfo.class, null);
            if (!H2Util.tableExist(entityName)){
                H2Util.createH2TableFromMongo(template, StockCodeInfo.class,null);
            }
            log.info("已经初始化表："+entityName);

            entityName = EntityUtil.getEntityNameByCode(Stock.class, null);
            if (!H2Util.tableExist(entityName)){
                H2Util.createH2TableFromMongo(template, Stock.class,null);
            }
            log.info("已经初始化表："+entityName);

        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return "";
    }



}
